package com.siberiadante.multiscrolldemo.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;

import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;


import net.lucode.hackware.magicindicator.MagicIndicator;


/**
 * @CopyRight: 广州仰望星空云科技有限公司
 * @Author : wuzuqing
 * @Email:wuzuqing@xinyu668.com
 * @CreateTime: 3.6.24 11:09 AM
 * @Description: 请写描述
 */
public class NestedRecycleView extends RecyclerView {
    public NestedRecycleView(Context context) {
        super(context);
        initParent();
    }


    public NestedRecycleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initParent();
    }

    public NestedRecycleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initParent();
    }

    private int pageTop;

    private void initParent() {
        post(new Runnable() {
            @Override
            public void run() {
                initScrollView();
                resetPageViewHeight();
            }
        });
    }

    private void resetPageViewHeight() {
        if (scrollView == null) {
            return;
        }
        ViewParent parent = getParent();
        int count = 0;
        ViewPager pager = null;
        while (parent != null && count < 20) {
            if (parent instanceof ViewPager) {
                pager = (ViewPager) parent;
                break;
            }
            parent = parent.getParent();
            count++;
        }
        int magicIndicatorHeight = 0;
        if (pager != null) {

            ViewParent viewParent = pager.getParent();
            if (viewParent instanceof ViewGroup) {
                ViewGroup ll = (ViewGroup) viewParent;
                int childCount = ll.getChildCount();
                for (int i = 0; i < childCount; i++) {
                    View child = ll.getChildAt(i);
                    if (child instanceof MagicIndicator) {
                        magicIndicatorHeight = child.getHeight();
                        break;
                    }
                }
            }
            //设置高度
            ViewGroup.LayoutParams params = pager.getLayoutParams();
            params.height = scrollView.getHeight() - scrollView.getToolBarHeight() - magicIndicatorHeight;
            pager.setLayoutParams(params);
        }
        getLocationOnScreen(location);

        pageTop = location[1] - scrollView.getToolBarHeight() - magicIndicatorHeight;
        Log.e("MyRecycleView",
                "resetPageViewHeight: pageTop=" + pageTop + ",toolBarPositionY=" + scrollView.getToolBarHeight());
    }

    private float lastMoveY;
    int[] location = new int[2];

    private void logEvent(float offset, float rcvTop, String tag) {
        Log.e("MyRecycleView",
                "onTouchEvent: " + tag + " = " + offset + ",getScrollY=" + scrollView.getScrollY() + ",rcvTop=" + rcvTop);
    }

    boolean isMoveDown;

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float rawY = event.getRawY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                lastMoveY = rawY;
                break;
            case MotionEvent.ACTION_MOVE:
                if (lastMoveY == 0) {
                    lastMoveY = rawY;
                    break;
                }
                getLocationOnScreen(location);
                float offset = lastMoveY - rawY;
                isMoveDown = rawY > lastMoveY;
                lastMoveY = rawY;
                if (scrollView != null) {
                    logEvent(offset, location[1], isMoveDown ? "下滑" : "上滑");
                    if (interceptTouch(isMoveDown, offset, location[1])) {
                        return false;
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
                getLocationOnScreen(location);
                if (!isMoveDown && !(scrollView.getScrollY() >= pageTop || location[1] == pageTop)) {
                    scrollView.setScrollY(pageTop);
                }
                lastMoveY = 0f;
                break;
            case MotionEvent.ACTION_CANCEL:
                lastMoveY = 0f;
                break;
        }
        return super.onTouchEvent(event);
    }

    public boolean interceptTouch(boolean isMoveDown, float offset, int rcvTop) {
        int scrollY = scrollView.getScrollY();
        if (isMoveDown) {
            if (scrollY <= 0 || rcvTop < pageTop) {
//                if (scrollY != 0) {
//                    scrollView.scrollTo(0, 0);
//                }
                return false;
            }
        } else {
            if (scrollY >= pageTop || rcvTop == pageTop) {
//                if (scrollY != pageTop) {
//                    scrollView.scrollTo(0, pageTop);
//                }
                return false;
            }
        }
        scrollView.scrollBy(0, (int) (offset + 0.5));
        return true;
    }

    JudgeNestedScrollView scrollView;

    private void initScrollView() {
        if (scrollView != null) {
            return;
        }
        ViewParent parent = getParent();
        int count = 0;
        while (parent != null && count < 20) {
            if (parent instanceof JudgeNestedScrollView) {
                scrollView = (JudgeNestedScrollView) parent;
                break;
            }
            parent = parent.getParent();
            count++;
        }
    }

}
